//
//  FirstViewController.m
//  TestDemo
//
//  Created by yutao on 14-2-11.
//  Copyright (c) 2014年 yutao. All rights reserved.
//

#import "FirstViewController.h"
#import "NavViewController.h"
#import "MyTableViewCell.h"

#define ImageURL @"http://su.bdimg.com/static/superpage/img/logo_white.png"    //定义常量的一种方式

@interface FirstViewController ()

@end

@implementation FirstViewController

@synthesize list=_list;

@synthesize addBtn;
@synthesize deleteBtn;
@synthesize showDialogBtn;
@synthesize tableView;
@synthesize jumpBtn;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.
    
    NSArray *array=[[NSArray alloc] initWithObjects:@"美国",@"中国",@"日本",nil];
    
    //self.list=array;
    
    NSMutableArray *array2=[[NSMutableArray alloc] initWithArray:array];
    self.list=array2;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction) addClick:(id)sender{
    NSLog(@"start Button click");
    
    [self.list addObject:@"菲律宾"];
    [tableView reloadData];     //刷新tableView 相当于android中adapter.notifyDataSetChanged()方法
    
}

-(IBAction) deleteClick:(id)sender{
    NSLog(@"end Button click");
    if([self.list count]>0){
        [self.list removeLastObject];
        [tableView reloadData];
    }
}

-(IBAction)showDialogClick:(id)sender{
    NSLog(@" show DialogClick ");
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"IOS学习" message:@"哈哈" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alert show];
}

-(IBAction)switchView:(id)sender{
    NSLog(@"切换视图");
    
   
    if(self.navController==nil){
        self.navController=[[NavViewController alloc] init];
    }
    
    [self presentViewController:self.navController animated:YES completion:nil];
    
    //UIView *nView=[[[NavViewController alloc] init] view];
    //[self.view insertSubview:nView atIndex:2];   //添加视图跟Index的层次有关系
    
}

-(void) downloadImage:(NSString *) url{
    NSData *data=[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
    UIImage *image=[[UIImage alloc] initWithData:data];
    if(image==nil){
        
    }else{
        //下载完成通知主线程刷新UI
        [self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];
    }
    
}

-(void) updateUI:(UIImage*) image{
    self.imageView.image=image;
}


// 调用协议A方法
-(IBAction) MethodAClick:(id)sender{
    
    //启动一个线程下载图片  @selector感觉相当于c#里面的delegate回调具体不清楚。
    NSThread *thread=[[NSThread alloc] initWithTarget:self selector:@selector(downloadImage:) object:ImageURL];
    [thread start];
    
    NSLog(@" MethodAClick 调用协议A方法");
    [self.navController methodA];
}



//实现tableView协议方法

//返回TableView中的cell的视图，相当于重写Adapter中的getView方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *TableSampleIdentifier=@"TableSampleIdentifier";
    NSUInteger row=[indexPath row];
    
    
    //自定义cell的View
//    MyTableViewCell* cell=(MyTableViewCell*)[tableView dequeueReusableCellWithIdentifier:TableSampleIdentifier];
//    if(cell==nil){
//        NSArray *nib=[[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:self options:nil];
//        cell=[nib objectAtIndex:0];
//
//    }
//    cell.nameLabel.text=[self.list objectAtIndex:row];
//    cell.imageView.image=[UIImage imageNamed:@"answer_number_1"];
   
   //系统自带cell
    UITableViewCell*  cell=[tableView dequeueReusableCellWithIdentifier:TableSampleIdentifier];
    if(cell==nil){
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableSampleIdentifier];
    }
    cell.textLabel.text=[self.list objectAtIndex:row];
    cell.imageView.image=[UIImage imageNamed:@"answer_number_1"];
    
    return cell;
}




//返回tableView中cell的个数相当于android Adapter中的getCount
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.list count];
}

//tableView的点击事件类似于android的setOnItemClickListener事件监听
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *rowString=[self.list objectAtIndex:[indexPath row]];
    UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"选中信息" message:rowString delegate:self
                                            cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
}


@end
